package com.ksm.zyz.controller;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ksm.zyz.common.base.BaseController;
import com.ksm.zyz.sys.entity.BsData;
import com.ksm.zyz.sys.entity.ExtData;
import com.ksm.zyz.sys.entity.OrderGoodsList;
import com.ksm.zyz.sys.entity.orderGoodsInfo;
import com.ksm.zyz.sys.serverentity.sOrderGoodsInfo;
import com.ksm.zyz.sys.serverentity.sOrderGoodsList;
import com.ksm.zyz.sys.serverservice.sOrderGoodsInfoService;
import com.ksm.zyz.sys.serverservice.sOrderGoodsListService;
import com.ksm.zyz.sys.service.orderGoodsInfoService;
import com.ksm.zyz.sys.service.orderGoodsListService;

/**
 * @author liuhao
 *
 */
@Controller
@RequestMapping("OrderGoodsTongBu")
public class OrderGoodsController extends BaseController<sOrderGoodsInfo> {

	@Autowired
	private sOrderGoodsInfoService sService;
	@Autowired
	private orderGoodsInfoService Service;
	@Autowired
	private sOrderGoodsListService sgoodslistService;
	@Autowired
	private orderGoodsListService ordergoodslistService;
	
	

	   @RequestMapping(value ="OrderGoodsInfo")
	    @ResponseBody
		public ExtData OrderGoodsInfo(int ming, HttpServletRequest req){
	         //List<sOrderGoodsInfo> list=sgoodsService.selectSome();
		   int scount=(int)sService.selectAllCount();
		   int count=(int)Service.selectCount(ming);
		   int begin=0;
		   if(scount>count)
		   {			   
			   begin=count;
		   }
		   else{
			   extData.setTotalCount(0);
			   return extData;
		   }
		   int size=2000;
		   for(int i=begin;i<=scount+size;i=i+size)
		   {
			   sOrderGoodsInfo sordergoodsinfo =new sOrderGoodsInfo();
			   BsData bsdata=new BsData();
			   bsdata.setBegin(i);
			   bsdata.setLimit(size);
			   sordergoodsinfo.setBsData(bsdata);
			   List<orderGoodsInfo> list=sService.selectAllInfo(sordergoodsinfo);
			   for(int j=0;j<list.size();j++)
			   {
				   orderGoodsInfo orderinfo=list.get(j);
				   System.out.println(list.get(j).getOrderOperator());
				   orderinfo.setSmId(ming);
				   orderinfo.setOrderNum(orderinfo.getOrderNum()+ming);				   				   
				  try
				   {
					   Service.insertSelective(orderinfo);					   
				   }
				   catch(Exception e){
					   System.out.println(orderinfo.getId()+"@@@@@"+orderinfo.getOrderNum());
					   extData.setTotalCount(i+j);
					   return extData;   
				   }
				   
			   }
			   extData.setTotalCount(scount-count);	  			   
		   }
		   
	               
			return extData;    
		}
	   @RequestMapping(value ="OrderGoodsList")
	    @ResponseBody
		public ExtData ordergoodslist(int ming, HttpServletRequest req){
	         //List<sOrderGoodsInfo> list=sgoodsService.selectSome();
		   int scount=(int)sgoodslistService.selectAllCount();
		   int count=(int)Service.selectCount(ming);
		   int begin=0;
		   if(scount>count)
		   {			   
			   begin=count;
		   }
		   else{
			   extData.setTotalCount(0);
			   return extData;
		   }
		   int size=2000;
		   for(int i=begin;i<=scount+size;i=i+size)
		   {
			   sOrderGoodsList sordergoodslist =new sOrderGoodsList();
			   BsData bsdata=new BsData();
			   bsdata.setBegin(i);
			   bsdata.setLimit(size);
			   sordergoodslist.setBsData(bsdata);
			   List<OrderGoodsList> list=sgoodslistService.selectAllInfo(sordergoodslist);
			   for(int j=0;j<list.size();j++)
			   {
				   OrderGoodsList orderlist=list.get(j);
				   System.out.println(list.get(j).getGoodsName());
				   orderlist.setSmId(ming);
				   orderlist.setOrderNum(orderlist.getOrderNum()+ming);	
				  try
				   {
					   ordergoodslistService.insertSelective(orderlist);					   
				   }
				   catch(Exception e){
					   System.out.println(orderlist.getId()+"@@@@@"+orderlist.getOrderNum());
					   extData.setTotalCount(i+j);
					   return extData;   
				   }
				   
			   }
			   extData.setTotalCount(scount-count);	  			   
		   }
		   
	               
			return extData;    
		}
	

}
